package com.game.modular.game.act.battle;

import com.game.comment.core.ResponseResult;
import com.game.comment.exection.ServiceException;
import com.game.comment.util.JwtUtils;
import com.game.modular.game.act.battle.vo.BattleVo;
import com.game.modular.game.playerAssets.domain.PvpRanks;
import com.game.modular.game.battle.info.roundInfo.domain.PvpBattleStatisticsInfo;
import com.game.modular.game.battle.process.PlayerPvpServiceImpl;
import com.game.modular.game.playerMatch.service.PlayerMatchServiceImpl;
import com.game.modular.game.playerMatch.service.PvpRanksServiceImpl;
import com.game.modular.game.playerAssets.service.PlayerHeroServiceImpl;
import com.game.modular.game.playerInfo.service.PlayerServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 武将队伍(PvpRanks)表服务控制层
 *
 * @author cxy
 * @since 2024-07-18 11:19:32
 */
@Api(tags = "pvp武将配队")
@Validated
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/pvpRanksAct")
public class PvpRanksAction {
    @Resource
    private PlayerHeroServiceImpl accountWuJiangService;
    @Resource
    private PvpRanksServiceImpl pvpRanksService;
    @Resource
    private PlayerPvpServiceImpl playerPvpService;
    @Resource
    private PlayerServiceImpl playerService;
    @Resource
    private PlayerMatchServiceImpl playerMatchServiceImpl;

    @ApiOperation("队伍列表")
    @PostMapping("/list")
    public ResponseResult<List<PvpRanks>> list(@RequestHeader String token) {
        Long playerId = JwtUtils.getPlayerId(token);
        return ResponseResult.success(pvpRanksService.list(playerId));
    }

    @ApiOperation("单队伍卸载")
    @PostMapping("/uninstallation")
    public ResponseResult<PvpRanks> uninstallation(
            @RequestHeader String token, @RequestBody BattleVo.PvpRanksVo.RankNo param

    ) {
        return ResponseResult.success(pvpRanksService.uninstallation(
                JwtUtils.getPlayerId(token), param.getRankNo())
        );
    }

    @ApiOperation("编队")
    @PostMapping("/formation")
    public ResponseResult<List<PvpRanks>> formation(
            @RequestHeader String token,
            @Valid @RequestBody BattleVo.PvpRanksVo.Formation param
    ) {


        Long playerId = JwtUtils.getPlayerId(token);
        Long rankNo = param.getRankNo();
        Long heroId1 = param.getHeroId1();
        Long heroId2 = param.getHeroId2();
        Long heroId3 = param.getHeroId3();
        HashSet<Long> longs = new HashSet<>();
        longs.add(4L);
        longs.add(7L);
        if (longs.contains(heroId1)|longs.contains(heroId2)|longs.contains(heroId3)){
            throw new ServiceException("当前武将不支持");
        }

        // 去重
        if (Objects.equals(heroId1, heroId2) && heroId1 != 0 || Objects.equals(heroId1, heroId3) && heroId1 != 0 || Objects.equals(heroId2, heroId3) && heroId2 != 0) {
            throw new ServiceException("请勿重复选择武将");
        }
        return ResponseResult.success(pvpRanksService.formation(playerId, rankNo, heroId1, heroId2, heroId3));
    }

    @ApiOperation("出战")
    @PostMapping("/clickBattle")
    public ResponseResult<PvpRanks> clickBattle(
            @RequestHeader("token") String token,
            @Valid @RequestBody BattleVo.PvpRanksVo.ToCityVo param
    ) {
        Long playerId = JwtUtils.getPlayerId(token);
        Long matchId = playerMatchServiceImpl.currentMatchId(playerId);
        if (matchId == 0) {
            throw new ServiceException("当前没有对局");
        }
        return ResponseResult.success(accountWuJiangService.clickBattle(
                playerId, matchId, param.getRankNo(), param.getTargetCityId())
        );
    }

    @ApiOperation("4. 队伍移动")
    @PostMapping("/teamMovement")
    public ResponseResult<PvpRanks> teamMovement(
            @RequestHeader("token") String token,
            @Valid @RequestBody BattleVo.PvpRanksVo.ToCityVo param
    ) {
        return ResponseResult.success("返回队伍,到达时间", pvpRanksService.teamMovement(
                JwtUtils.getPlayerId(token), param.getRankNo(), param.getTargetCityId())
        );
    }

    @ApiOperation("5. 攻打npc城池")
    @PostMapping("/attackNpcCity")
    public ResponseResult<Map<Long, List<PvpBattleStatisticsInfo>>> attackNpcCity(
            @RequestHeader("token") String token,
            @Valid @RequestBody BattleVo.PvpRanksVo.ToCityVo param
    ) {
        return ResponseResult.success(playerPvpService.attackNpcCity(
                JwtUtils.getPlayerId(token), param.getRankNo(), param.getTargetCityId())
        );
    }

    @ApiOperation("攻打城池,玩家/npc ")
    @PostMapping("/attackCity")
    public ResponseResult<Map<Long, List<PvpBattleStatisticsInfo>>> attackCity(
            @RequestHeader("token") String token,
            @Valid @RequestBody BattleVo.PvpRanksVo.ToCityVo param
    ) {
        return ResponseResult.success(playerPvpService.attackCity(
                JwtUtils.getPlayerId(token), param.getRankNo(), param.getTargetCityId())
        );
    }
}
